package com.cl.edu.web.admin;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.cl.core.web.struts2.Struts2Utils;
import com.cl.edu.entity.Admin;
import com.cl.edu.service.admin.AdminManager;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
@Results( { @Result(name = "admin", location = "admin.jsp", type = "redirect") ,
		 @Result(name = "pass", location = "password.jsp", type = "dispatcher") })
public class LoginAction extends ActionSupport
{

	@Autowired
	private AdminManager adminManager;

	private String			username;
	private String			password;
	
	private Integer id;

	public String execute() throws Exception
	{
		Admin admin = new Admin();
		admin.setUsername(username);
		admin.setPassword(password);

		if (adminManager.hasUsername(admin)) {
			admin = adminManager.isExist(admin);
			if (admin != null) {
				Struts2Utils.getSession().setAttribute("admin", admin);
				return "admin";
			} else {
				addActionMessage("密码不正确");
				return SUCCESS;
			}
		} else {
			addActionMessage("用户名不存在!");
			return SUCCESS;
		}

	}
	
	//修改密码
	public String changeps() throws Exception {
		Admin entity = adminManager.getAdmin(id);
		entity.setPassword(Struts2Utils.getParameter("new_password"));
		try {
			adminManager.updateAdmin(entity);
			Struts2Utils.getSession().setAttribute("admin",entity);
			addActionMessage("您的密码修改成功,请下次登录时使用新密码!");
		} catch (Exception e) {
			addActionMessage("您的密码修改失败!");
			e.printStackTrace();
		}
		return "pass";
	}

	public String getUsername()
	{
		return username;
	}

	public void setUsername(String username)
	{
		this.username = username;
	}

	public String getPassword()
	{
		return password;
	}

	public void setPassword(String password)
	{
		this.password = password;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

}